import log from './rollup/log'
import del from 'del'
import replace from '@rollup/plugin-replace'
import commonjs from '@rollup/plugin-commonjs'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import babel from '@rollup/plugin-babel'
import typescript from 'rollup-plugin-typescript2'
import { terser } from "rollup-plugin-terser"
import license from 'rollup-plugin-license'

del.sync(['lib/**', 'umd/**']);

const cjs = {
  output: {
    file: 'lib/index.js',
    format: 'cjs', // 模块化
  }
};
const umd = {
  output: {
    file: 'umd/index.js', // 输出文件名
    format: 'umd', // 模块化
    name: 'reduxLocalStorageIframe', // 本模块的全局变量名
  }
};
const config = {
  input: 'src/index.ts',
  plugins: [
    log(), // 编译日志
    replace({ // 设置生产环境
      'process.env.NODE_ENV': '"production"',
      preventAssignment: false,
    }),
    nodeResolve({
      extensions: ['.ts', '.mjs', '.js', '.json', '.node']
    }), // 引入文件
    commonjs(), // commonjs转换为ES6
    typescript({ // 处理ts
      clean: true, // 缓存
      useTsconfigDeclarationDir: true, // 使用ts文件配置
    }),
    babel({ // 高级语法转换
      extensions: [
        '.js',
        '.jsx',
        '.ts',
        '.tsx'
      ],
      babelHelpers: 'bundled',
    }),
    terser(), // 压缩精简
    license({ // 写上版本
      banner: (`
        Copyright <%= moment().format('YYYY') %> dsh.
        version <%= pkg.version %>.
      `)
    })
  ],
};

export default [
  { ...config, ...cjs },
  { ...config, ...umd },
];
